package com.peas.platform.module.icims.budget.apply.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.peas.platform.common.core.enums.BaseEnum;
import com.peas.platform.framework.mybatisplus.domain.BaseDomain;
import com.peas.platform.framework.mybatisplus.po.BasePo;
import com.peas.platform.framework.mybatisplus.service.BaseServiceImpl;
import com.peas.platform.module.icims.budget.apply.api.req.SaveBudgetTwoCollectVo;
import com.peas.platform.module.icims.budget.apply.controller.vo.BudgetTwoCollectRelationVo;
import com.peas.platform.module.icims.budget.apply.domain.BudgetTwoCollectDomain;
import com.peas.platform.module.icims.budget.apply.dto.BudgetTwoCollectDto;
import com.peas.platform.module.icims.budget.apply.dto.BudgetTwoCollectRelationDto;
import com.peas.platform.module.icims.budget.apply.dto.BudgetTwoDto;
import com.peas.platform.module.icims.budget.apply.enums.BudgetCollectSync;
import com.peas.platform.module.icims.budget.apply.enums.BudgetCollectType;
import com.peas.platform.module.icims.budget.apply.service.BudgetTwoCollectRelationService;
import com.peas.platform.module.icims.budget.apply.service.BudgetTwoCollectService;
import com.peas.platform.module.icims.budget.apply.service.BudgetTwoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

@Service
public class BudgetTwoCollectServiceImpl extends BaseServiceImpl<BudgetTwoCollectDto> implements BudgetTwoCollectService {

    @Resource
    private BudgetTwoCollectDomain budgetTwoCollectDomain;
    @Resource
    private BudgetTwoCollectRelationService budgetTwoCollectRelationService;
    @Resource
    private BudgetTwoService budgetTwoService;

    @Override
    public BaseDomain<BudgetTwoCollectDto, ? extends BasePo> getDomain() {
        return budgetTwoCollectDomain;
    }

    @Transactional
    @Override
    public void saveBudgetTwoCollect(SaveBudgetTwoCollectVo vo) {
        BudgetTwoCollectDto dto = BeanUtil.copyProperties(vo, BudgetTwoCollectDto.class);
        BudgetCollectType type = BaseEnum.key2Enum(BudgetCollectType.class, vo.getBudgetTwoCollectType());
        dto.setYsySync(BudgetCollectSync.nosync.getKey());
        if (type == BudgetCollectType.sync) {
            dto.setYsySync(BudgetCollectSync.unsync.getKey());
        }
        switch (type) {
            case sync:
            case collect:
                int result = dto.getId() == null ? this.save(dto) : this.updateById(dto);
                Assert.isTrue(result > 0, "保存失败");
                List<BudgetTwoCollectRelationDto> bocrds = budgetTwoCollectRelationService.listByField("budgetTwoCollectId", dto.getId());
                budgetTwoCollectRelationService.removeBatchByList(bocrds);
                List<BudgetTwoCollectRelationDto> bocrdList = new ArrayList<>();
                CollUtil.forEach(vo.getItems(), (CollUtil.Consumer<BudgetTwoCollectRelationVo>) (v, index) -> {
                    BudgetTwoCollectRelationDto bocrd = new BudgetTwoCollectRelationDto();
                    bocrd.setBudgetTwoCollectId(dto.getId());
                    bocrd.setBudgetTwoId(v.getBudgetTwoId());
                    bocrdList.add(bocrd);
                });
                budgetTwoCollectRelationService.saveBatch(bocrdList);
                break;
            case by_one:
                //TODO
                break;
            case by_org:
                //TODO
                break;
            case by_apply:
                //TODO
                break;
            case by_use:
                //TODO
                break;
        }
    }

    @Override
    public BigDecimal calcBudgetTwoCollectTotalAmount(BudgetTwoCollectDto dto) {
        if (dto == null) {
            return BigDecimal.ZERO;
        }
        List<BudgetTwoCollectRelationDto> bocrds = budgetTwoCollectRelationService.listByField("budgetTwoCollectId", dto.getId());
        if (CollUtil.isEmpty(bocrds))
            return BigDecimal.ZERO;
        AtomicReference<BigDecimal> all = new AtomicReference<>(BigDecimal.ZERO);
        CollUtil.forEach(bocrds, (CollUtil.Consumer<BudgetTwoCollectRelationDto>) (bocrd, index) -> {
            BudgetTwoDto bod = budgetTwoService.getById(bocrd.getBudgetTwoId());
            BigDecimal sum = all.get().add(budgetTwoService.calcBudgetTwoTotalAmount(bod));
            all.set(sum);
        });
        dto.setTotalAmount(all.get());
        return all.get();
    }

}
